<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>使用递归实现深拷贝</title>
</head>

<body>
  <script>
    function deepClone(obj) {
      if (obj === null || typeof obj !== 'object') return obj;
      if (obj instanceof Boolean) return new Boolean(obj.valueOf());
      if (obj instanceof Number) return new Number(obj.valueOf());
      if (obj instanceof String) return new String(obj.valueOf());
      if (obj instanceof RegExp) return new RegExp(obj.valueOf());
      if (obj instanceof Date) return new Date(obj.valueOf());
      var cpObj = obj instanceof Array ? [] : {};
      for (var key in obj) {
        cpObj[key] = deepClone(obj[key]);
      }
      return cpObj;
    }

    const obj = {
      a: 1,
      b: 'str',
      c: () => { },
      d: [1, 2, 3],
      e: undefined
    }
    const copyObj = deepClone(obj);
    console.log('obj---', obj);
    console.log('copyObj---', copyObj)
  </script>
</body>

</html>